package com.example.demo.service.user;

import com.example.demo.commom.PasswordUtils;
import com.example.demo.commom.TokenUtil;
import com.example.demo.dto.user.UserUpdateDto;
import com.example.demo.dto.user.login.ULoginDto;
import com.example.demo.dto.user.reg.URegDto;
import com.example.demo.entity.user.ClassInfo;
import com.example.demo.entity.user.Student;
import com.example.demo.mapper.user.ClassMapper;
import com.example.demo.mapper.user.StudentMapper;
import com.example.demo.vo.user.LoginVo;
import com.example.demo.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;

    public Integer reg(URegDto regDto) {
        // 密码加密
        regDto.setPassword(PasswordUtils.encrypt(regDto.getPassword()));
        // 判断班级是否存在
        if(regDto.getClass_id() != null){
            ClassInfo classInfo = classMapper.getClassInfoByClassId(regDto.getClass_id());
            if(classInfo == null){
                regDto.setClass_id(-1);
            }
        }
        // 判断身份证号是否重复
        if(studentMapper.getStudentByCardId(regDto.getCard_id()) != null){
           return 0;
        }
        //注册
        return studentMapper.reg(regDto);
    }

    // 登录
    public LoginVo login(ULoginDto uLoginDto) {
        //校验是否可以进行登录操作
        Student student = studentMapper.getStudentById(uLoginDto.getUser_id());
        if (student == null || student.getStudent_id() < 10000 || student.getStudent_id() > 100000) {
            return LoginVo.returnInfo(-1, "学号或密码错误");
        }
        if (student.getState() > 2 && student.getUnfreezing_time() > System.currentTimeMillis()) {
            Long waitTime = (student.getUnfreezing_time() - System.currentTimeMillis()) / 1000;
            return LoginVo.returnInfo(-1, "请等待"+waitTime+"s后重试");
        }
        if (student.getState() > 2 && student.getUnfreezing_time() <= System.currentTimeMillis()) {
            student.setState(0);
            studentMapper.upStudentState(student);
        }
        //密码加盐处理过
        if (PasswordUtils.check(uLoginDto.getPassword(), student.getPassword())) {
            student.setState(0);
            studentMapper.upStudentState(student);
            return LoginVo.returnInfo(0, TokenUtil.getToken(student.getStudent_id()));
        } else {
            //1、先将state修改
            student.setState(student.getState() + 1);
            //2、是否修改取消冻结的最新时间
            if (student.getState() > 2) {
                student.setUnfreezing_time(System.currentTimeMillis() + 20000);//当前时间往后家100s
            }
            //更新state+unfreezingtime
            studentMapper.upStudentState(student);
            return LoginVo.returnInfo(-1, "学号或密码输入错误");
        }
    }

    // 验证学生身份
    public Boolean authentication(Integer user_id){
        Student student = studentMapper.getStudentById(user_id);
        if (student == null || student.getStudent_id() < 10000 || student.getStudent_id() > 100000) {
            return false;
        }
        return true;
    }

    // 查看学生信息  0表示查看全部学生，1表示根据班级查看，2表示根据专业查看，其他值表示查看指定用户信息
    public List<UserInfoVo> getStudentInfo(Integer student_id, Integer class_id, String major){
        List<UserInfoVo> infos = new ArrayList<>();
        if(student_id == 0){
            // 查看所有学生信息
            List<Student> students = studentMapper.getAllStudent();
            for(int i = 0;i<students.size();i++){
                ClassInfo classInfo = new ClassInfo();
                if(students.get(i).getClass_id() != null){
                    classInfo = classMapper.getClassInfoByClassId(students.get(i).getClass_id());
                }
                infos.add(UserInfoVo.createVoByStudent(students.get(i), classInfo));
            }
        } else if(student_id == 1) {
            // 根据班级查看
            List<Student> students = studentMapper.getStudentByClassId(class_id);
            for(int i = 0;i<students.size();i++){
                ClassInfo classInfo = new ClassInfo();
                if(students.get(i).getClass_id() != null){
                    classInfo = classMapper.getClassInfoByClassId(students.get(i).getClass_id());
                }
                infos.add(UserInfoVo.createVoByStudent(students.get(i), classInfo));
            }
        } else if(student_id == 2) {
            // 根据专业查看
            List<Student> students = studentMapper.getStudentByMajor(major);
            for(int i = 0;i<students.size();i++){
                ClassInfo classInfo = new ClassInfo();
                if(students.get(i).getClass_id() != null){
                    classInfo = classMapper.getClassInfoByClassId(students.get(i).getClass_id());
                }
                infos.add(UserInfoVo.createVoByStudent(students.get(i), classInfo));
            }
        }else {
            // 查看指定学生信息
            Student student = studentMapper.getStudentById(student_id);
            if(student == null){
                return null;
            }
            ClassInfo classInfo = new ClassInfo();
            if(student.getClass_id() != null && student.getClass_id() > 0){
                classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            }
            infos.add(UserInfoVo.createVoByStudent(student, classInfo));
        }
        return infos;
    }


    // 修改学生信息
    public String  updateStudentInfo(UserUpdateDto student, Integer id) {
        Student user = studentMapper.getStudentById(id);
        if(user == null) {
            return "用户不存在";
        }
        //如果需要修改密码，需要判断旧密码是否正确
        if(!student.getOldpassword().isEmpty() && !student.getPassword().isEmpty()
        && !PasswordUtils.check(student.getOldpassword(),user.getPassword())){
            return "旧密码有误";
        }
        //开始正式修改
        //密码加盐
        if(!student.getPassword().isEmpty()) {
            user.setPassword(PasswordUtils.encrypt(student.getPassword()));
        }
        // 其他信息修改
        if(StringUtils.hasLength(student.getEmail())){
            user.setEmail(student.getEmail());
        }
        if(StringUtils.hasLength(student.getPhone())){
            user.setPhone(student.getPhone());
        }
        return String.valueOf(studentMapper.updateStudentInfo(user));

    }

}
